/**
 * Created by Administrator on 2017/12/8.
 * 抽离购物车公共js服务
 * ***点击图片加入购物车调用原生动画效果服务**
 * **购物车增、删、查、改后的事件更新服务**
 * **其他业务模块列表同步购车数据服务**
 */

import storage from 'utils/storage';
import notification from 'utils/notification';
import constant from 'constants/constant';

const globalEvent = weex.requireModule('globalEvent');

/**
 * @description 存储购物车信息到原生storage
 * @param data 存储的相关数据
 */
function storageCartInfo(data) {
    data = data || {};
    storage.setItem("cartInfo",data);
}


/**
 * @description 发送购物车操作后的事件通知和storage存储
 * @param cartData 操作后后台返回的最新购物车数据
 * @param goods 操作当前商品的商品数据
 */
function sendEvent(cartData,goods) {
    cartData = cartData || {};
    goods = goods || {};

    let _data = {
        totalNum: cartData.totalNum || 0, //购物车数量
        totalItems: cartData.totalItems || 0, //购物车种类
        totalMoney: cartData.totalMoney || 0, //购物车总价格
        totalCheckedNum: cartData.totalCheckedNum || 0, //购物车选中商品的总数量
        totalCheckedMoney: cartData.totalCheckedMoney || 0 //购物车选中商品的总价格
    };

    //存储本地购物车数量
    storageCartInfo(_data);

    //dispatch更新购物车事件
    //通知原生
    notification.sendToNative(constant.EVENT_UPDATE_CART,_data);
    //通知其他模块,传递购物车数据是为了通用模块底部的结算
    notification.send(constant.EVENT_UPDATE_CART,_data);
    //通知其他模块，传递相应的商品数据是为了同步其他模块对应的商品数量
    notification.send(constant.EVENT_GOODS_RESUME,goods);
}

/**
 * @description 点击图片加入购物车调用原生动画效果
 * @param data 要传递给原生的执行加入购物车动画的参数
 */
function addCartAnimation(data) {
    notification.sendToNative(constant.EVENT_ADD_CART_ANIMATION,data);
}


/**
 * @description 同步购物车商品数据
 * @param vm 绑定当前的上下文
 */
function resumeCartGoods(vm) {
    //同步操作单个购物车商品
    globalEvent.addEventListener(constant.EVENT_GOODS_RESUME, event => {
        if(event) {
            let _goods = event.data || {};
            if(vm.$refs['ref' + _goods.id]) {
                let len = vm.$refs['ref' + _goods.id].length;
                for(let i=0; i < len; i++) {
                    let el = vm.$refs['ref' + _goods.id][i];
                    if(el && el.goods) el.goods.cart_num = _goods.cart_num;
                }
            }
        }
    });

    //监听购物车删除事件并同步商品数据(单个删除，勾选部分删除，全部删除)
    globalEvent.addEventListener(constant.EVENT_CART_DELETE, event => {
        //遍历删除的购物车列表 event为删除商品id的集合
        let _cartGoodsList = event.data || [];
        if(_cartGoodsList.length) {
            _cartGoodsList.forEach(function(v,i,arr){
                if(vm.$refs['ref' + v]) {
                    let len = vm.$refs['ref' + v].length;
                    for(let i=0; i < len; i++) {
                        let el = vm.$refs['ref' + v][i];
                        if(el && el.goods) el.goods.cart_num = 0; //重置为0
                    }
                }
            });
        }
    });
}

/**
 * @description 移除同步购物车商品数据的绑定监听
 */
function removeResumeCartGoodsListener() {
    globalEvent.removeEventListener(constant.EVENT_GOODS_RESUME);
    globalEvent.removeEventListener(constant.EVENT_CART_DELETE);
}


/**
 * @description 底部bar监听购物车change事件
 * @description 监听购物车新增、更新、删除、勾选事件
 * @description 监听登录事件
 * @description 监听购物车生成订单事件
 * @param callback 回调函数
 */
function cartAddEventListener(callback){
    //监听更新购物车事件
    globalEvent.addEventListener(constant.EVENT_UPDATE_CART, function (e) {
        e = e.data || {};
        if(typeof callback == "function") callback(e);
    });

    //监听购物车删除事件
    globalEvent.addEventListener(constant.EVENT_CART_DELETE_FOR_FOOTER, function (e) {
        e = e.data || {};
        let result = {};
        result.totalNum = e.totalNum || 0; //购物车数量
        result.totalItems = e.totalItems || 0; //购物车种类
        result.totalMoney = e.totalMoney || 0; //购物车总价格
        result.totalCheckedNum = e.totalCheckedNum || 0; //购物车选中商品的总数量
        result.totalCheckedMoney = e.totalCheckedMoney || 0; //购物车选中商品的总价格

        if(typeof callback == "function") callback(result);
    });

    //监听购物车勾选事件
    globalEvent.addEventListener(constant.EVENT_CART_CHECKED, function (e) {
        e = e.data || {};
        let result = {};
        result.totalNum = e.totalNum || 0; //购物车数量
        result.totalItems = e.totalItems || 0; //购物车种类
        result.totalMoney = e.totalMoney || 0; //购物车总价格
        result.totalCheckedNum = e.totalCheckedNum || 0; //购物车选中商品的总数量
        result.totalCheckedMoney = e.totalCheckedMoney || 0; //购物车选中商品的总价格

        if(typeof callback == "function") callback(result);
    });

    //监听登录后更新购物车数量
    globalEvent.addEventListener(constant.EVENT_GET_LOGIN, function (e) {
        e = e || {};
        if(typeof callback == "function") callback(e);
    });

    //监听购物车商品生成订单事件
    globalEvent.addEventListener(constant.EVENT_CART_TO_ORDER, function (e) {
        e = e.data || {};
        let result = {};
        result.totalNum = e.totalNum || 0; //购物车数量
        result.totalItems = e.totalItems || 0; //购物车种类
        result.totalMoney = e.totalMoney || 0; //购物车总价格
        result.totalCheckedNum = e.totalCheckedNum || 0; //购物车选中商品的总数量
        result.totalCheckedMoney = e.totalCheckedMoney || 0; //购物车选中商品的总价格

        if(typeof callback == "function") callback(result);
    });
}

/**
 * @description 移除底部bar购物车的绑定监听
 */
function removeCartAddEventListener() {
    globalEvent.removeEventListener(constant.EVENT_UPDATE_CART);
    globalEvent.removeEventListener(constant.EVENT_CART_DELETE_FOR_FOOTER);
    globalEvent.removeEventListener(constant.EVENT_CART_CHECKED);
    globalEvent.removeEventListener(constant.EVENT_GET_LOGIN);
    globalEvent.removeEventListener(constant.EVENT_CART_TO_ORDER);
}


exports.storageCartInfo = storageCartInfo;
exports.sendEvent = sendEvent;
exports.addCartAnimation = addCartAnimation;
exports.resumeCartGoods = resumeCartGoods;
exports.removeResumeCartGoodsListener = removeResumeCartGoodsListener;
exports.cartAddEventListener = cartAddEventListener;
exports.removeCartAddEventListener = removeCartAddEventListener;
